package servlet.vehicle;

import bean.dto.VehicleChangeData;
import bean.pojo.User;
import bean.pojo.Vehicle;
import com.alibaba.fastjson2.JSONObject;
import commons.baseServlet.BaseServlet;
import commons.format.NumberFormat;
import commons.r.R;
import service.ParkingSpaceService;
import service.VehicleService;
import service.impl.ParkingSpaceServiceImpl;
import service.impl.VehicleServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * servlet.vehicle
 * User: 坚持
 * Date: 2024/12/19 14:12
 * motto: 树欲静而风不止
 * Description:
 * Version: V1.0
 */
@WebServlet(name="VehicleManagementServlet",value = "/vehicle/*")
public class VehicleManagementServlet extends BaseServlet {
    /**
     * 车辆信息添加
     * @param request
     * @return
     */
    public R add(HttpServletRequest request){
        try {
            BufferedReader bufferedReader=request.getReader();
            String req_data=bufferedReader.readLine();
            Vehicle vehicle = JSONObject.parseObject(req_data,Vehicle.class);
            VehicleService visitorService = new VehicleServiceImpl();
            Boolean add=visitorService.add(vehicle);
            return add?R.ok():R.error("添加失败");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error("出错");
    }

    /**
     * 根据车辆ID删除车辆信息
     * @param request
     * @return
     */
    public R deleteById(HttpServletRequest request){
        Integer vehicleId = NumberFormat.stringToInteger(request.getParameter("vehicleId"));
        VehicleService visitorService = new VehicleServiceImpl();
        Boolean delete=visitorService.deleteById(vehicleId);
        return delete?R.ok():R.error("删除失败");
    }

    /**
     * 修改车辆信息
     * @param request
     * @return
     */
    public R update(HttpServletRequest request){
        try {
            BufferedReader bufferedReader = request.getReader();
            String req_data=bufferedReader.readLine();
            VehicleChangeData vehicleChangeData = JSONObject.parseObject(req_data,VehicleChangeData.class);
            VehicleService vehicleService = new VehicleServiceImpl();
            ParkingSpaceService parkingSpaceService = new ParkingSpaceServiceImpl();

            Boolean updateVehicle = vehicleService.updateVehicle(vehicleChangeData);
            Boolean updateParkingSpace = parkingSpaceService.updateParkingSpace(vehicleChangeData);

            //在Dao层或者Service层判断是否回滚事务
            return updateVehicle&&updateParkingSpace?R.ok():R.error("修改失败");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error("出错");


    }

    /**
     * 查询车辆的信息
     * @param request
     * @return
     */
    public R findBy(HttpServletRequest request){
        Integer page = NumberFormat.stringToInteger(request.getParameter("page"));
        Integer limit = NumberFormat.stringToInteger(request.getParameter("limit"));
        String vehicleLicensePlate = request.getParameter("vehicleLicensePlate");
        String ownerName = request.getParameter("ownerName");
        String vehicleType = request.getParameter("vehicleType");

        VehicleService vehicleService = new VehicleServiceImpl();
        Long total = vehicleService.findByTotal(vehicleLicensePlate,ownerName,vehicleType);
        List<VehicleChangeData> list=null;
        if(total>0) {
            //当前页码的数据
            list = vehicleService.findBy(page,limit,vehicleLicensePlate,ownerName,vehicleType);
        }
        return R.ok().addData("total",total).addData("list",list);

    }
}
